﻿Imports System.Windows.Forms

Public Class ThamSo

    Private bus_thamSo As New BUS.ThamSo
    Private dto_thamSo As DTO.ThamSo

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        If luu() Then
            Me.DialogResult = System.Windows.Forms.DialogResult.OK
            Me.Close()
        End If
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub Apply_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Apply_Button.Click
        luu()
    End Sub

    Private Sub ThamSo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dto_thamSo = bus_thamSo.LayThamSo
        'tui chua xet truong hop ko lay dc, ong bo xung nha, ma biet loi o dau nua
        tb_thoigianbaytoithieu.Text = dto_thamSo.ThoiGianBayToiThiru
        tb_slsanbaytrunggiantoida.Text = dto_thamSo.SL_SanBayTrungGianToiDa
        tb_thoigiannghitu.Text = dto_thamSo.ThoiGianNghiTaiSanBayTu
        tb_thoigiannghiden.Text = dto_thamSo.ThoiGianNghiTaiSanBayDen
        Apply_Button.Enabled = False
    End Sub

    Private Function luu() As Boolean
        If Not Integer.TryParse(tb_thoigianbaytoithieu.Text, dto_thamSo.ThoiGianBayToiThiru) Then
            MessageBox.Show("""Thời gian bay tối thiểu"" không đúng định dạng", "Lỗi", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error)
            Return False
        End If
        If Not Integer.TryParse(tb_slsanbaytrunggiantoida.Text, dto_thamSo.SL_SanBayTrungGianToiDa) Then
            MessageBox.Show("""Số lượng sân bay trung gian"" không đúng định dạng", "Lỗi", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error)
            Return False
        End If
        If Not Integer.TryParse(tb_thoigiannghitu.Text, dto_thamSo.ThoiGianNghiTaiSanBayTu) Then
            MessageBox.Show("""Thời gian nghỉ tại san bay trung gian từ"" không đúng định dạng", "Lỗi", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error)
            Return False
        End If
        If Not Integer.TryParse(tb_thoigiannghiden.Text, dto_thamSo.ThoiGianNghiTaiSanBayDen) Then
            MessageBox.Show("""Thời gian nghỉ tại sân bay trung gian đến"" không đúng định dạng", "Lỗi", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error)
            Return False
        End If
        bus_thamSo.LuuThamSo(dto_thamSo)
        Apply_Button.Enabled = False
        Return True
    End Function

    Private Sub tb_thoigianbaytoithieu_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tb_thoigianbaytoithieu.TextChanged, tb_slsanbaytrunggiantoida.TextChanged, tb_thoigiannghitu.TextChanged, tb_thoigiannghiden.TextChanged
        Apply_Button.Enabled = True
    End Sub

End Class
